package com.intelligent.health.setmeal.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.intelligent.health.setmeal.dao.SetMealMapper;
import com.intelligent.health.setmeal.dto.SetMealDto;
import com.intelligent.health.setmeal.pojo.CheckGroup;
import com.intelligent.health.setmeal.service.SetMealService;
import com.intelligent.health.setmeal.pojo.SetMeal;
import com.intelligent.health.setmeal.vo.SetMealVo;
import entity.PageInfo;
import entity.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: torlesse-liang
 * @Date: 2022/03/13/22:38
 * @Description:
 */
@Slf4j
@Service
public class SetMealServiceImpl implements SetMealService {

    @Autowired
    private SetMealMapper setMealMapper;

    @Override
    public PageResult<SetMealVo> querySetMealsByPage(SetMealDto setMealDto, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper queryWrapper = getLambdaQueryWrapper(setMealDto);
        IPage<SetMeal> setMeals = setMealMapper.selectPage(new Page<>(pageNum, pageSize), queryWrapper);
        PageResult<SetMealVo> pageResult = new PageResult<>();
        ArrayList<SetMealVo> setMealVos = new ArrayList<>();
        setMeals.getRecords().forEach(setMeal -> {
            SetMealVo setMealVo = new SetMealVo();
            BeanUtils.copyProperties(setMeal, setMealVo);
            setMealVos.add(setMealVo);
        });
        pageResult.setPageSize(setMeals.getSize());
        pageResult.setTotal(setMeals.getTotal());
        pageResult.setCurrentPage(setMeals.getCurrent());
        pageResult.setRows(setMealVos);
        return pageResult;
    }

    @Override
    public SetMeal querySetMealById(Integer setMealId) {
        return setMealMapper.selectById(setMealId);
    }

    @Override
    public void addSetMeal(HashMap<String, Object> hashMap) {
        //添加套餐
        SetMeal setMeal = JSONObject.parseObject(JSON.toJSONString(hashMap.get("setMeal")), SetMeal.class);
        setMealMapper.insert(setMeal);
        //往t_setmeal_checkgroup中间表添加记录
        ArrayList<Integer> checkGroupIds = JSONObject.parseObject(JSON.toJSONString(hashMap.get("checkGroupIds")), ArrayList.class);
        setMealMapper.addBatchCheckGroupToSetMeal(setMeal.getId(), checkGroupIds);
    }

    @Override
    public void editSetMeal(HashMap<String, Object> hashMap) {
        //添加套餐
        SetMeal setMeal = JSONObject.parseObject(JSON.toJSONString(hashMap.get("setMeal")), SetMeal.class);
        setMealMapper.updateById(setMeal);
        //删除中间表记录
        setMealMapper.delAllCheckGroupsBySetMealId(setMeal.getId());
        //往t_setmeal_checkgroup中间表添加记录
        ArrayList<Integer> checkGroupIds = JSONObject.parseObject(JSON.toJSONString(hashMap.get("checkGroupIds")), ArrayList.class);
        setMealMapper.addBatchCheckGroupToSetMeal(setMeal.getId(), checkGroupIds);
    }

    @Override
    public void delSetMeal(Integer setMealId) {
        setMealMapper.delSetMeal(setMealId);
    }

    @Override
    public List<SetMeal> querySetMeals(HashMap<String, Object> hashMap) {
        List<SetMeal> setMeals = setMealMapper.querySetMealsForExcel(hashMap);
        return setMeals;
    }

    @Override
    public void delBatchSetMeal(ArrayList<Integer> setMealIds) {
        setMealMapper.delBatchSetMeal(setMealIds);
    }

    @Override
    public List<CheckGroup> queryCheckGroupsBySid(Integer setMealId) {
        return setMealMapper.queryCheckGroupsBySid(setMealId);
    }

    public LambdaQueryWrapper getLambdaQueryWrapper(SetMealDto setMealDto) {
        LambdaQueryWrapper<SetMeal> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(setMealDto.getCode())) {
            queryWrapper.like(SetMeal::getCode, setMealDto.getCode());
        }
        if (StringUtils.isNotBlank(setMealDto.getName())) {
            queryWrapper.like(SetMeal::getName, setMealDto.getName());
        }
        if (StringUtils.isNotBlank(setMealDto.getSex())) {
            queryWrapper.eq(SetMeal::getSex, setMealDto.getSex());
        }
        if (StringUtils.isNotBlank(setMealDto.getHelpCode())) {
            queryWrapper.like(SetMeal::getHelpCode, setMealDto.getHelpCode());
        }
        if (StringUtils.isNotBlank(setMealDto.getAge())) {
            queryWrapper.eq(SetMeal::getAge, setMealDto.getAge());
        }
        //前端默认有效 1
        if (setMealDto.getState() != null) {
            queryWrapper.eq(SetMeal::getState, setMealDto.getState());
        }
        return queryWrapper;
    }
}
